<template>
  <div>
    <el-dialog
      title="注册资源"
      :visible.sync="register.isShow"
      style="width: 100%"
      @open="resetform"
    >
      <el-form
        ref="ResourcePubform"
        :model="ResourcePubform"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="算力" prop="cpu">
          <el-col :span="22">
            <el-input v-model="ResourcePubform.cpu"></el-input>
          </el-col>GHz
        </el-form-item>
        <el-form-item label="内存" prop="memory">
          <el-col :span="16">
            <el-input v-model.number="ResourcePubform.memory"> </el-input>
          </el-col>
          <el-col :span="6">
            <el-select
              v-model="ResourcePubform.memUnit"
              placeholder="内存单位"
              aria-required
            >
              <el-option label="MB" value="MB"></el-option>
              <el-option label="GB" value="GB"></el-option>
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="硬盘" prop="harddisk">
          <el-col :span="16">
            <el-input v-model.number="ResourcePubform.harddisk"> </el-input>
          </el-col>
          <el-col :span="6">
            <el-select
              v-model="ResourcePubform.harddiskUnit"
              placeholder="硬盘单位"
            >
              <el-option label="MB" value="MB"></el-option>
              <el-option label="GB" value="GB"></el-option>
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="网络带宽" prop="network">
          <el-col :span="16">
            <el-input v-model.number="ResourcePubform.network"> </el-input>
          </el-col>
          <el-col :span="6"> Mbps </el-col>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="handleCancel">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "RegisterResource",
  props: ["register", "ethAddr"],
  data() {
    var checkNum = (rule, value, callback) => {
      if (!Number.isInteger(value)) {
        callback(new Error("请输入数字值"));
      } else {
        callback();
      }
    };
    return {
      ResourcePubform: {
        cpu: "",
        memory: "",
        harddisk: "",
        network: "",
        price: "",
        desc: "",
        memUnit: "GB",
        harddiskUnit: "GB",
        ethaddr: this.ethAddr[0],
      },
      rules: {
        cpu: [{ required: true, message: "请输入CPU描述", trigger: "blur" }],
        memory: [
          { required: true, message: "请输入内存大小", trigger: "blur" },
          { validator: checkNum, trigger: "blur" },
        ],
        harddisk: [
          { required: true, message: "请输入硬盘大小", trigger: "blur" },
          { validator: checkNum, trigger: "blur" },
        ],
        network: [
          { required: true, message: "请输入带宽大小", trigger: "blur" },
        ],
        price: [
          { required: true, message: "请输入价格", trigger: "blur" },
          { validator: checkNum, trigger: "blur" },
        ],
        desc: [{ required: true, message: "请输入资源描述", trigger: "blur" }],
        memUnit: [
          { required: true, message: "请输入内存单位", trigger: "blur" },
        ],
        harddiskUnit: [
          { required: true, message: "请输入硬盘单位", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    resetform() {
      this.ResourcePubform.cpu = "";
      this.ResourcePubform.memory = "";
      this.ResourcePubform.network = "";
      this.ResourcePubform.desc = "";
      this.ResourcePubform.price = "";
      this.ResourcePubform.harddisk = "";
      this.ResourcePubform.ethaddr = this.ethAddr[0];
    },
    handleCancel() {
      // eslint-disable-next-line vue/no-mutating-props
      //console.log('s');
      // eslint-disable-next-line vue/no-mutating-props
      this.register.isShow = false;
      this.ResourcePubform.cpu = "";
      this.ResourcePubform.memory = "";
      this.ResourcePubform.network = "";
      this.ResourcePubform.desc = "";
      this.ResourcePubform.price = "";
      this.ResourcePubform.harddisk = "";
    },
    confirm() {
      this.$refs.ResourcePubform.validate((valid) => {
        if (valid) {
          this.GLOBAL.web3.eth.getAccounts().then((res) => {
            var tmp = this.GLOBAL.contract.methods
              .createResource(
                this.$route.query.username,
                this.ResourcePubform.ethaddr,
                this.ResourcePubform.price,
                this.ResourcePubform.cpu,
                this.ResourcePubform.memory.toString() +
                  this.ResourcePubform.memUnit,
                this.ResourcePubform.harddisk.toString() +
                  this.ResourcePubform.harddiskUnit,
                this.ResourcePubform.network.toString(),
                this.ResourcePubform.desc
              )
              .send({ from: res[0] })
              .then((res) => {
                console.log(res);
              });
            console.log("999", tmp);
          });
          //this.dialogVisible = false;
          console.log(this.ResourcePubform.price, this.ResourcePubform.ethaddr);
        } else {
          this.$message.error("请正确输入！");
        }
      });
    },
  },
};
</script>
